package 常用类.String类;

public class string元素反转 {
    public static void main(String[] args) {
        String str = "abcdef";
        try{
            System.out.println(reverse(str, 1, 4));
        }catch (RuntimeException e){
            System.out.println(e);
        }


    }

    public static String reverse(String  str, int start, int end){
        char[] chars = str.toCharArray();
        char temp = ' ';
        if(!(str != null && start >=0 && start < end && end < str.length())){
            throw new RuntimeException("参数异常");
        }
        for(int i = start,j = end;i < j; i++,j--){
             temp = chars[i];
             chars[i] = chars[j];
             chars[j] = temp;
        }
        return new String(chars);
    }
}
